﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.ServiceModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using PmuRegistry.Silverlight.ModalDialogs;
using PmuRegistry.Silverlight.PmuRegistryDataServiceProxy;
using PmuRegistry.Silverlight.Utilities;

namespace PmuRegistry.Silverlight.Pages.Devices
{
	public partial class InputWizard : Page
	{
		PhasorDataServiceClient m_client;
		Stream m_configFileData, m_iniFileData;		
		ObservableCollection<WizardDeviceInfo> m_wizardDeviceInfoList;
		Dictionary<int, string> m_vendorDeviceList;
		Dictionary<int, string> m_deviceStatuses;
		Dictionary<string, string> m_phasorTypes;
		Dictionary<string, string> m_phaseTypes;
		int? m_parentID = null;
		string m_iniFileName = string.Empty;
		string m_iniFilePath = string.Empty;
		ActivityWindow m_activityWindow;

		public InputWizard()
		{
			InitializeComponent();
			//Services Events			
			m_client = Common.GetPhasorDataServiceClient();
			m_client.GetDeviceStatusesCompleted += new EventHandler<GetDeviceStatusesCompletedEventArgs>(client_GetDeviceStatusesCompleted);
			m_client.GetProtocolsCompleted += new EventHandler<GetProtocolsCompletedEventArgs>(client_GetProtocolsCompleted);
			m_client.GetVendorDevicesCompleted += new EventHandler<GetVendorDevicesCompletedEventArgs>(client_GetVendorDevicesCompleted);
			m_client.GetCompaniesCompleted += new EventHandler<GetCompaniesCompletedEventArgs>(client_GetCompaniesCompleted);			
			m_client.GetInterconnectionsCompleted += new EventHandler<GetInterconnectionsCompletedEventArgs>(client_GetInterconnectionsCompleted);			
			m_client.GetWizardConfigurationInfoCompleted += new EventHandler<GetWizardConfigurationInfoCompletedEventArgs>(client_GetWizardConfigurationInfoCompleted);
			m_client.SaveWizardConfigurationInfoCompleted += new EventHandler<SaveWizardConfigurationInfoCompletedEventArgs>(client_SaveWizardConfigurationInfoCompleted);
			m_client.GetDeviceByAcronymCompleted += new EventHandler<GetDeviceByAcronymCompletedEventArgs>(client_GetDeviceByAcronymCompleted);
			m_client.SaveDeviceCompleted += new EventHandler<SaveDeviceCompletedEventArgs>(client_SaveDeviceCompleted);
			m_client.GetExecutingAssemblyPathCompleted += new EventHandler<GetExecutingAssemblyPathCompletedEventArgs>(client_GetExecutingAssemblyPathCompleted);
			m_client.SaveIniFileCompleted += new EventHandler<SaveIniFileCompletedEventArgs>(client_SaveIniFileCompleted);
			//Controls Events
			Loaded += new RoutedEventHandler(InputWizard_Loaded);
			ButtonBrowseConfigurationFile.Click += new RoutedEventHandler(ButtonBrowseConfigurationFile_Click);			
			ButtonBrowseIniFile.Click += new RoutedEventHandler(ButtonBrowseIniFile_Click);
			ButtonNext.Click += new RoutedEventHandler(ButtonNext_Click);
			ButtonPrevious.Click += new RoutedEventHandler(ButtonPrevious_Click);
			AccordianWizard.SelectionChanged += new SelectionChangedEventHandler(AccordianWizard_SelectionChanged);
			CheckboxConnectToPDC.Checked += new RoutedEventHandler(CheckboxConnectToPDC_Checked);
			CheckboxConnectToPDC.Unchecked += new RoutedEventHandler(CheckboxConnectToPDC_Unchecked);
			ComboboxProtocol.SelectionChanged += new SelectionChangedEventHandler(ComboboxProtocol_SelectionChanged);
		}

		void client_GetDeviceStatusesCompleted(object sender, GetDeviceStatusesCompletedEventArgs e)
		{
			if (e.Error == null)
				m_deviceStatuses = e.Result;
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Device Statuses", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
		}

		#region " Service Event Handlers"

		void client_SaveIniFileCompleted(object sender, SaveIniFileCompletedEventArgs e)
		{			
			if (e.Error == null)
				m_iniFileName = e.Result;
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Upload INI File", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();			
			}			
		}

		void client_GetExecutingAssemblyPathCompleted(object sender, GetExecutingAssemblyPathCompletedEventArgs e)
		{			
			if (e.Error == null)
				m_iniFilePath = e.Result;			
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Current Execution Path", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
		}

		void client_GetInterconnectionsCompleted(object sender, GetInterconnectionsCompletedEventArgs e)
		{
			if (e.Error == null)
				ComboboxInterconnection.ItemsSource = e.Result;
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Interconnections", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
			if (ComboboxInterconnection.Items.Count > 0)
				ComboboxInterconnection.SelectedIndex = 0;
		}

		void client_GetCompaniesCompleted(object sender, GetCompaniesCompletedEventArgs e)
		{
			if (e.Error == null)
				ComboboxCompany.ItemsSource = e.Result;
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Companies", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
			if (ComboboxCompany.Items.Count > 0)
				ComboboxCompany.SelectedIndex = 0;
		}
		
        void client_GetVendorDevicesCompleted(object sender, GetVendorDevicesCompletedEventArgs e)
		{
			if (e.Error == null)
			{
				m_vendorDeviceList = e.Result;
				ComboboxPDCVendor.ItemsSource = m_vendorDeviceList;
			}
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Vendor Devices", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
			if (ComboboxPDCVendor.Items.Count > 0)
				ComboboxPDCVendor.SelectedIndex = 0;
		}
		
        void client_GetProtocolsCompleted(object sender, GetProtocolsCompletedEventArgs e)
		{
			if (e.Error == null)
				ComboboxProtocol.ItemsSource = e.Result;
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Protocols", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
			if (ComboboxProtocol.Items.Count > 0)
				ComboboxProtocol.SelectedIndex = 0;
		}
		
		void client_GetWizardConfigurationInfoCompleted(object sender, GetWizardConfigurationInfoCompletedEventArgs e)
		{
			if (e.Error == null)
			{
				m_wizardDeviceInfoList = e.Result;
				ItemControlDeviceList.ItemsSource = m_wizardDeviceInfoList;
			}
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Parse Configuration File", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
            if (m_activityWindow != null)
                m_activityWindow.Close();
		}
		
        void client_SaveWizardConfigurationInfoCompleted(object sender, SaveWizardConfigurationInfoCompletedEventArgs e)
		{
			SystemMessages sm;
			if (e.Error == null)
			{				
				sm = new SystemMessages(new Message() { UserMessage = e.Result, SystemMessage = string.Empty, UserMessageType = MessageType.Success },
						ButtonType.OkOnly);
			}
			else
			{
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Save Configuration Information", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
			}
			sm.Show();
			if (m_activityWindow != null)
				m_activityWindow.Close();	
		}
		
        void client_GetDeviceByAcronymCompleted(object sender, GetDeviceByAcronymCompletedEventArgs e)
		{
			if (e.Error == null)
			{
				Device device = new Device();
				device = e.Result;
				if (device != null && device.IsConcentrator)
					m_parentID = device.ID;
				else	// means PDC does not exist. if (parentID == null)
				{
					App app = (App)Application.Current;
					device = new Device();
					device.Name = TextBoxPDCName.Text;
					device.Acronym = TextBoxPDCAcronym.Text;
					device.SubstationAcronym = string.Empty;
					device.SubstationName = string.Empty;
					device.Longitude = 0;
					device.Latitude = 0;
					device.TimeZone = string.Empty;
					device.InterconnectionID = ((KeyValuePair<int, string>)ComboboxInterconnection.SelectedItem).Key;
					device.CompanyID = ((KeyValuePair<int, string>)ComboboxCompany.SelectedItem).Key;
					device.VendorDeviceID = ((KeyValuePair<int, string>)ComboboxPDCVendor.SelectedItem).Key;
					device.ProtocolID = ((KeyValuePair<int, string>)ComboboxProtocol.SelectedItem).Key;
					device.MeasuredLines = 0;
					device.IsConcentrator = true;
					device.ParentID = null;
					device.StatusID = 1;
					device.StatusDate = DateTime.Now;
					device.TechnicalContactID = null;
					device.InServiceDate = null;
					m_client.SaveDeviceAsync(device, true, 0, 0);
				}
			}
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Retrieve Device Information by Acronym", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
		}
		
        void client_SaveDeviceCompleted(object sender, SaveDeviceCompletedEventArgs e)
		{
			if (e.Error == null)
				m_client.GetDeviceByAcronymAsync(TextBoxPDCAcronym.Text);	// calling this again would set parentID needed in the final step of the wizard.
			else
			{
				SystemMessages sm;
				if (e.Error is FaultException<CustomServiceFault>)
				{
					FaultException<CustomServiceFault> fault = e.Error as FaultException<CustomServiceFault>;
					sm = new SystemMessages(new Message() { UserMessage = fault.Detail.UserMessage, SystemMessage = fault.Detail.SystemMessage, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);
				}
				else
					sm = new SystemMessages(new Message() { UserMessage = "Failed to Save Device Information", SystemMessage = e.Error.Message, UserMessageType = MessageType.Error },
						ButtonType.OkOnly);

				sm.Show();
			}
		}

		#endregion

		#region " Controls Event Handlers"

		void ComboboxProtocol_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			//If BPA PDC Stream protocol is selected, then ask users to upload their INI file.
			if (((KeyValuePair<int, string>)ComboboxProtocol.SelectedItem).Value.ToUpper().Contains("BPA"))
				IniFileUploadVisualization(Visibility.Visible);
			else
				IniFileUploadVisualization(Visibility.Collapsed);
		}
		
        void CheckboxConnectToPDC_Unchecked(object sender, RoutedEventArgs e)
		{
			PdcInfoVisualization(Visibility.Collapsed);
		}
		
        void CheckboxConnectToPDC_Checked(object sender, RoutedEventArgs e)
		{
			PdcInfoVisualization(Visibility.Visible);
		}
		
        void AccordianWizard_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (AccordianWizard.SelectedIndex == 1)
			{
				if (m_wizardDeviceInfoList.Count == 0) //this was added to fix issues with going back and forth on wizard steps.
				{
                    m_activityWindow = new ActivityWindow("Validating Configuration File... Please Wait...");
                    m_activityWindow.Show();

					if ((((KeyValuePair<int, string>)ComboboxProtocol.SelectedItem).Value.ToUpper().Contains("BPA")) && !string.IsNullOrEmpty(m_iniFileName))
					{
						string configFileDataString = (new StreamReader(m_configFileData)).ReadToEnd();
						string leftPart = configFileDataString.Substring(0, configFileDataString.IndexOf("</configurationFileName>"));
						string rightPart = configFileDataString.Substring(configFileDataString.IndexOf("</configurationFileName>"));
						leftPart = leftPart.Substring(0, leftPart.LastIndexOf(">") + 1);

						configFileDataString = leftPart + m_iniFilePath + "\\" + m_iniFileName + rightPart;

						Byte[] fileData = Encoding.UTF8.GetBytes(configFileDataString);
						MemoryStream ms = new MemoryStream();
						ms.Write(fileData, 0, fileData.Length);
						ms.Position = 0;
						m_client.GetWizardConfigurationInfoAsync(ReadFileBytes(ms));
					}
					else
						m_client.GetWizardConfigurationInfoAsync(ReadFileBytes(m_configFileData));
				}
			}

			if (AccordianWizard.SelectedIndex == 2 && ((bool)CheckboxConnectToPDC.IsChecked))
				m_client.GetDeviceByAcronymAsync(TextBoxPDCAcronym.Text.Replace(" ", "").ToUpper());


			if (AccordianWizard.SelectedIndex == 0)
			{
				ButtonPrevious.Visibility = Visibility.Collapsed;
				ButtonNext.Tag = "Next";
			}
			else if (AccordianWizard.SelectedIndex == AccordianWizard.Items.Count - 1)
			{
				ButtonPrevious.Visibility = Visibility.Visible;
				ButtonNext.Tag = "Finish";
			}
			else
			{
				ButtonNext.Tag = "Next";
				ButtonPrevious.Visibility = Visibility.Visible;
			}
		}
		
        void ButtonBrowseIniFile_Click(object sender, RoutedEventArgs e)
		{
			Storyboard sb = new Storyboard();
			sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
			sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
			Storyboard.SetTarget(sb, ButtonBrowseIniFileTransform);
			sb.Begin();

			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Multiselect = false;
			openFileDialog.Filter = "INI Files (*.ini)|*.ini|All Files (*.*)|*.*";
			bool? result = openFileDialog.ShowDialog();
			if (result != null && result == true)
			{
				TextBoxIniFile.Text = openFileDialog.File.Name;
				m_iniFileName = openFileDialog.File.Name;
				m_iniFileData = openFileDialog.File.OpenRead();
				m_client.SaveIniFileAsync(ReadFileBytes(m_iniFileData));				
			}
		}		
		
        void ButtonBrowseConfigurationFile_Click(object sender, RoutedEventArgs e)
		{
			Storyboard sb = new Storyboard();
			sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
			sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
			Storyboard.SetTarget(sb, ButtonBrowseConfigurationFileTransform);
			sb.Begin();

			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Multiselect = false;
			openFileDialog.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
			bool? result = openFileDialog.ShowDialog();
			if (result != null && result == true)
			{
				TextBoxConfigurationFile.Text = openFileDialog.File.Name;
				m_configFileData = openFileDialog.File.OpenRead();			
			}
		}
		
        void ButtonPrevious_Click(object sender, RoutedEventArgs e)
		{
			Storyboard sb = new Storyboard();
			sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
			sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
			Storyboard.SetTarget(sb, ButtonPreviousTransform);
			sb.Begin();

			if (AccordianWizard.SelectedIndex > 0)
			{
				AccordionItem item = AccordianWizard.Items[AccordianWizard.SelectedIndex - 1] as AccordionItem;
				item.IsSelected = true;
			}
		}
		
        void ButtonNext_Click(object sender, RoutedEventArgs e)
		{
			Storyboard sb = new Storyboard();
			sb = Application.Current.Resources["ButtonPressAnimation"] as Storyboard;
			sb.Completed += new EventHandler(delegate(object obj, EventArgs es) { sb.Stop(); });
			Storyboard.SetTarget(sb, ButtonNextTransform);
			sb.Begin();
            
			if (AccordianWizard.SelectedIndex == AccordianWizard.Items.Count - 1)
			{
				m_activityWindow = new ActivityWindow("Processing Request... Please Wait...");
				m_activityWindow.Show();

				App app = (App)Application.Current;
				int protocolID = ((KeyValuePair<int, string>)ComboboxProtocol.SelectedItem).Key;
				int companyID = ((KeyValuePair<int, string>)ComboboxCompany.SelectedItem).Key;				
				int interconnectionID = ((KeyValuePair<int, string>)ComboboxInterconnection.SelectedItem).Key;
				m_client.SaveWizardConfigurationInfoAsync(m_wizardDeviceInfoList, protocolID, companyID, interconnectionID, m_parentID);
			}

			if (AccordianWizard.SelectedIndex < AccordianWizard.Items.Count - 1)
			{
				AccordionItem item = AccordianWizard.Items[AccordianWizard.SelectedIndex + 1] as AccordionItem;
				item.IsSelected = true;
			}
		}
		
        void ComboboxVendor_Loaded(object sender, RoutedEventArgs e)
		{
			if (m_vendorDeviceList.Count > 0)
			{
				((ComboBox)sender).ItemsSource = m_vendorDeviceList;
				((ComboBox)sender).SelectedIndex = 0;
			}
		}
		
        void ComboboxType_Loaded(object sender, RoutedEventArgs e)
		{
			ComboBox phasorTypes = (ComboBox)sender;
			phasorTypes.ItemsSource = m_phasorTypes;
			try
			{
				PhasorInfo dataContext = new PhasorInfo();
				dataContext = phasorTypes.DataContext as PhasorInfo;
				foreach (KeyValuePair<string, string> item in m_phasorTypes)
				{
					if (item.Key == dataContext.Type)
					{
						phasorTypes.SelectedItem = item;
						break;
					}
				}
			}
			catch
			{
				//we don't care
			}
		}
		
        void ComboboxPhase_Loaded(object sender, RoutedEventArgs e)
		{
			ComboBox phaseTypes = (ComboBox)sender;
			phaseTypes.ItemsSource = m_phaseTypes;
			try
			{
				PhasorInfo dataContext = new PhasorInfo();
				dataContext = phaseTypes.DataContext as PhasorInfo;
				foreach (KeyValuePair<string, string> item in m_phaseTypes)
				{
					if (item.Key == dataContext.Phase)
					{
						phaseTypes.SelectedItem = item;
						break;
					}
				}
			}
			catch
			{
				//we don't care
			}
		}
		
        void ComboboxStatus_Loaded(object sender, RoutedEventArgs e)
		{
			if (m_deviceStatuses.Count > 0)
			{
				((ComboBox)sender).ItemsSource = m_deviceStatuses;
				((ComboBox)sender).SelectedIndex = 0;
			}
		}

        void CheckAllDevices_Checked(object sender, RoutedEventArgs e)
        {
            if (m_wizardDeviceInfoList != null)
            {
                foreach (WizardDeviceInfo deviceInfo in m_wizardDeviceInfoList)
                {
                    deviceInfo.Include = true;
                }
            }
        }

        void CheckAllDevices_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (WizardDeviceInfo deviceInfo in m_wizardDeviceInfoList)
            {
                deviceInfo.Include = false;
            }
        }

        void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            WizardDeviceInfo deviceInfo = (WizardDeviceInfo)((CheckBox)sender).DataContext;
            foreach (PhasorInfo phasorInfo in deviceInfo.PhasorList)
            {
                phasorInfo.Include = true;
            }
        }

        void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            WizardDeviceInfo deviceInfo = (WizardDeviceInfo)((CheckBox)sender).DataContext;
            foreach (PhasorInfo phasorInfo in deviceInfo.PhasorList)
            {
                phasorInfo.Include = false;
            }
        }

		#endregion

		void InputWizard_Loaded(object sender, RoutedEventArgs e)
		{			
			PdcInfoVisualization(Visibility.Collapsed);
			if (AccordianWizard.SelectedIndex == 0)
				ButtonPrevious.Visibility = Visibility.Collapsed;			
		}

		void PdcInfoVisualization(Visibility visibility)
		{
			TextBlockPDCName.Visibility = visibility;
			TextBoxPDCName.Visibility = visibility;
			TextBlockAcronym.Visibility = visibility;
			TextBoxPDCAcronym.Visibility = visibility;
			TextBlockPDCDeviceVendor.Visibility = visibility;
			ComboboxPDCVendor.Visibility = visibility;
		}
		
        void IniFileUploadVisualization(Visibility visibility)
		{
			TextBlockIniFile.Visibility = visibility;
			StackPanelIniFile.Visibility = visibility;
		}
		
        byte[] ReadFileBytes(Stream inputStream)
		{
			byte[] bytes;
			bytes = new byte[inputStream.Length];
			int numBytesToRead = (int)inputStream.Length;
			int numBytesRead = 0;
			while (numBytesToRead > 0)
			{
				// Read may return anything from 0 to numBytesToRead.
				int n = inputStream.Read(bytes, numBytesRead, numBytesToRead);

				// Break when the end of the file is reached.
				if (n == 0)
					break;

				numBytesRead += n;
				numBytesToRead -= n;
			}
			numBytesToRead = bytes.Length;			
			return bytes;
		}

		// Executes when the user navigates to this page.
		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			m_wizardDeviceInfoList = new ObservableCollection<WizardDeviceInfo>();
			m_vendorDeviceList = new Dictionary<int, string>();
			m_deviceStatuses = new Dictionary<int, string>();
			m_client.GetExecutingAssemblyPathAsync();
			m_client.GetProtocolsAsync(false);
			m_client.GetVendorDevicesAsync(true);
			m_client.GetDeviceStatusesAsync();
			m_client.GetCompaniesAsync(true);
			m_client.GetInterconnectionsAsync(true);

			m_phaseTypes = new Dictionary<string, string>();
			m_phaseTypes.Add("+", "Positive");
			m_phaseTypes.Add("-", "Negative");
			m_phaseTypes.Add("A", "Phase A");
			m_phaseTypes.Add("B", "Phase B");
			m_phaseTypes.Add("C", "Phase C");

			m_phasorTypes = new Dictionary<string, string>();
			m_phasorTypes.Add("V", "Voltage");
			m_phasorTypes.Add("I", "Current");
		}

	}
}
